#include "application.h"
#include "widget.h"

#include <QtWebKit>

#define MSG_UNIT (1024)
void myMessageOutput(QtMsgType type, const char *msg)
 {
     //in this function, you can write the message to any stream!
     switch (type) {
     case QtDebugMsg:
         //fprintf(stderr, "Debug: %s\n", msg);
     {
         size_t v_sizelen = strlen(msg);
         char v_buff[MSG_UNIT+1];
         for(size_t i=0; i<v_sizelen; i+=MSG_UNIT)
         {
             strncpy(v_buff, msg, MSG_UNIT);
             v_buff[MSG_UNIT]= '\0';
             fprintf(stderr, "%s", v_buff);
             fflush(stderr);
             msg += strlen(v_buff);
         }
         fprintf(stderr, "\n");
         fflush(stderr);
     }
         break;
     case QtWarningMsg:
         fprintf(stderr, "Warning: %s\n", msg);
         break;
     case QtCriticalMsg:
         fprintf(stderr, "Critical: %s\n", msg);
         break;
     case QtFatalMsg:
         fprintf(stderr, "Fatal: %s\n", msg);
         abort();
     }
 }

int main(int argc, char *argv[])
{
    Q_INIT_RESOURCE(jslib);
    qInstallMsgHandler(myMessageOutput);
    Application a(argc, argv);

    QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));
    QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"));

    QCoreApplication::setOrganizationDomain("akio.miyoshi");
    QCoreApplication::setOrganizationName("akio.miyoshi");
    QCoreApplication::setApplicationName("yt_list");
    QSettings settings(
            QSettings::IniFormat, //QSettings::NativeFormat,
            QSettings::UserScope, // QSettings::SystemScope,
            QCoreApplication::organizationName(),
            QCoreApplication::applicationName()
            );

    QWebSettings *webSettings = QWebSettings::globalSettings();
    webSettings->setAttribute(QWebSettings::JavascriptEnabled, true);
    webSettings->setAttribute(QWebSettings::PluginsEnabled, true);
    webSettings->setAttribute(QWebSettings::LocalContentCanAccessRemoteUrls, true);

    //webSettings->setAttribute(QWebSettings::LocalContentCanAccessFileUrls, true);


    Widget w;
    w.show();

    return a.exec();
}
